package com.jinzhi.jzweb.controller;


import java.text.SimpleDateFormat;
import java.util.*;

import com.jinzhi.jzweb.domain.MsgDO;
import com.jinzhi.jzweb.service.MsgService;
import com.jinzhi.sys.domain.UserDO;
import com.jinzhi.sys.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.jzweb.domain.TrainInfoDO;
import com.jinzhi.jzweb.service.TrainInfoService;
import com.jinzhi.common.utils.Result;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2021-01-13 14:27:41 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/trainInfo")
public class TrainInfoController extends BaseController {
	@Autowired
	private TrainInfoService trainInfoService;
	@Autowired
	private MsgService msgService;
	@Autowired
	private UserService userService;
	@GetMapping()
	@RequiresPermissions("jzweb:trainInfo:trainInfo")
	String TrainInfo(){
	    return "jzweb/trainInfo/trainInfo";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:trainInfo:trainInfo")
	public Result<Page<TrainInfoDO>> list(TrainInfoDO trainInfoDTO){
		// 查询列表数据
		Page<TrainInfoDO> page = trainInfoService.selectPage(getPage(TrainInfoDO.class),
					trainInfoService.convertToEntityWrapper().like("type",trainInfoDTO.getType()==null?null:trainInfoDTO.getType().toString()).like("enterprise_name",trainInfoDTO.getEnterpriseName()).orderBy("id", false));

		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
				TrainInfoDO obj = (TrainInfoDO) page.getRecords().get(i);
			UserDO userDO = userService.selectById(obj.getExamineUser());
			if(userDO!=null){
				obj.setExamineUserName(userDO.getUsername());
			}

			UserDO userDO1 = userService.selectById(obj.getCreateUser());
			if(userDO1!=null){
				obj.setCreateUserName(userDO1.getUsername());
			}

			list.add(obj);
		}
		page.setRecords(list);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("jzweb:trainInfo:add")
	String add(){
	    return "jzweb/trainInfo/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:trainInfo:edit")
	String edit(@PathVariable("id") Integer id,Model model){
		TrainInfoDO trainInfo = trainInfoService.selectById(id);
		model.addAttribute("trainInfo", trainInfo);
	    return "jzweb/trainInfo/edit";
	}
	
	@Log("添加")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:trainInfo:add")
	public Result<String> save( TrainInfoDO trainInfo){
		trainInfo.setCreateTime(new Date());
		trainInfo.setState(0);

		trainInfo.setType(1);
		//trainInfoService.insert(trainInfo);


        return Result.ok();
	}

	@Log("保存草稿")
	@ResponseBody
	@PostMapping("/saveDraft")
	@RequiresPermissions("jzweb:trainInfo:add")
	public Result<String> saveDraft( TrainInfoDO trainInfo){
		trainInfo.setCreateTime(new Date());
		trainInfo.setState(0);
		trainInfo.setType(0);
		trainInfoService.insert(trainInfo);
		return Result.ok();
	}
	
	@Log("修改")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:trainInfo:edit")
	public Result<String>  update( TrainInfoDO trainInfo){
		boolean update = trainInfoService.updateById(trainInfo);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:trainInfo:remove")
	public Result<String>  remove( Integer id){
		trainInfoService.deleteById(id);
        return Result.ok();
	}
	
	@Log("批量删除")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:trainInfo:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Integer[] ids){
		trainInfoService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}


	/**
	 * 培训站点审核
	 * @param ids
	 * @param state  1通过  2不通过
	 * @param remark  描述
	 * @return
	 */
	@Log("审核")
	@PostMapping( "/review")
	@ResponseBody
	@RequiresPermissions("jzweb:trainInfo:review")
	public Result<String>  review(@RequestParam("ids[]") Long[] ids,Integer state,String remark) {

		if (state.equals(2) || state.equals(1)) {
			int i = 0;
			for (Long id : ids) {
				String valStr="";
				MsgDO msg = new MsgDO();
				//查询这条数据ID
				TrainInfoDO trainInfoDO= trainInfoService.selectById(id);
				trainInfoDO.setExamineUser(this.getUserId());
				trainInfoDO.setExamineTime(new Date());
				SimpleDateFormat sdf=new SimpleDateFormat();

				if(state.equals(2)){
					trainInfoDO.setType(3);
					trainInfoDO.setExamineRemarks(remark);//未通过，添加备注
					msg.setTitle("培训基地审核未通过！");
					valStr="您申请的培训基地审核未通过，"+"审核人"+this.getUser().getUsername()+"审核时间："+sdf.format(new Date())+"不通过原因"+remark;
				}else{
					trainInfoDO.setType(2);
					trainInfoDO.setExamineRemarks("");//审核通过，清空备注
					msg.setTitle("培训基地审核通过！");
					valStr="您申请的培训基地审核通过！"+"审核人"+this.getUser().getUsername()+"审核时间："+sdf.format(new Date());
				}

				boolean b = trainInfoService.updateById(trainInfoDO);

				if(b){

					msg.setContent(valStr);
					msg.setUserId(trainInfoDO.getCreateUser());
					msg.setCreaterDate(new Date());
//						msg.setEnclosure();
					msg.setType(0);
					msg.setMsgType(0);
					msg.setState(0);
					msg.setCreaterId(this.getUserId());
					msgService.insert(msg);
					i++;
				}
			}

			if (i == ids.length) {
				return Result.build(0,"已成功审核：" + i + "条数据");
			} else {
				return Result.fail("审核状态修改失败！");
			}

		}

		else {
			return Result.fail("审核状态修改失败！");
		}
	}

	/**
	 * 详情
	 * @param id
	 * @param model
	 * @return
	 */
	@GetMapping("/details")
	@RequiresPermissions("jzweb:trainInfo:trainInfo")
	String details( Long id,Model model){
		TrainInfoDO trainInfo = trainInfoService.selectById(id);

		UserDO userDO = userService.selectById(trainInfo.getExamineUser());
		if(userDO!=null){
			trainInfo.setExamineUserName(userDO.getUsername());
		}


		UserDO userDO1 = userService.selectById(trainInfo.getCreateUser());
		if(userDO1!=null){
			trainInfo.setCreateUserName(userDO1.getUsername());
		}
		model.addAttribute("trainInfo", trainInfo);
		return "jzweb/trainInfo/details";
	}
}
